# 人工智能NLP-Agent数字人项目-02-日程提醒任务工单V1.1-20250217
import sqlite3
from typing import Any
from langchain.tools import BaseTool


class QueryTimerDB(BaseTool):
    name: str = "QueryTimerDB"
    description: str = ("用于查询所有日程，返回的数据里包含3个参数:"
                        "时间、循环规则（如:'1000100'代表星期一和星期五循环，'0000000'代表不循环）、执行的事项")

    def __init__(self):
        super().__init__()

    async def _arun(self, *args: Any, **kwargs: Any) -> Any:
        # 用例中没有用到 arun 不予具体实现
        pass

    def _run(self, para: dict) -> str:
        query = "SELECT * FROM timer"
        params = []

        if 'uid' in para:
            query += " WHERE uid = ?"
            params.append(para['uid'])

        with sqlite3.connect('timer.db') as conn:
            cursor = conn.cursor()
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS timer (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    time TEXT NOT NULL,
                    repeat_rule TEXT NOT NULL,
                    content TEXT NOT NULL,
                    uid INTEGER NOT NULL
                )
            ''')
            cursor.execute(query, params)
            rows = cursor.fetchall()

        result = "\n".join(str(row) for row in rows)
        return result


if __name__ == "__main__":
    tool = QueryTimerDB()
    result = tool.run({'uid': 1})  # 示例参数
    print(result)

